/**
* Copyright (c) 2013-2014, qtone Inc. All Rights Reserved.
*
* @author: tanghuaicheng
* Project Name: testuc
* $Id:   2017-01-11 10:54:07 $
*  拼接sql查询条件。
*/

package com.bangcommunity.bbframe.mybatis.criteria;

import com.bangcommunity.bbframe.common.utils.lang.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public abstract class AbstractorCriteria<C extends BaseGeneratedCriteria> implements Serializable {
    protected String orderByClause;

    protected boolean distinct;

    protected List<C> oredCriteria;

    protected Integer offSet;

    protected Integer pageSize;

    private static final long serialVersionUID = 1L;

    public AbstractorCriteria() {
        oredCriteria = new ArrayList<C>();
    }

    public void setOrderByClause(String orderByClause) {
        this.orderByClause = orderByClause;
    }

    public String getOrderByClause() {
        return orderByClause;
    }

    public AbstractorCriteria orderDesc(String field) {
        if (this.orderByClause == null) {
            this.orderByClause = "";
        }
        if(StringUtils.isNotBlank(this.orderByClause)){
            this.orderByClause += ",";
        }
        this.orderByClause += " " + field + " desc";
        return this;
    }

    public AbstractorCriteria orderAsc(String field) {
        if (this.orderByClause == null) {
            this.orderByClause = "";
        }
        if(StringUtils.isNotBlank(this.orderByClause)){
            this.orderByClause += ",";
        }
        this.orderByClause += " " + field;
        return this;
    }

    public void setDistinct(boolean distinct) {
        this.distinct = distinct;
    }

    public boolean isDistinct() {
        return distinct;
    }

    public List<C> getOredCriteria() {
        return oredCriteria;
    }

    public void or(C criteria) {
        oredCriteria.add(criteria);
    }

    public C or() {
        C criteria = createCriteriaInternal();
        oredCriteria.add(criteria);
        return criteria;
    }

    public C createCriteria() {
        C criteria = createCriteriaInternal();
        if (oredCriteria.size() == 0) {
            oredCriteria.add(criteria);
        }
        return criteria;
    }

    protected abstract C createCriteriaInternal();

    public void clear() {
        oredCriteria.clear();
        orderByClause = null;
        distinct = false;
    }

    public void setOffSet(Integer offSet) {
        this.offSet = offSet;
    }

    public Integer getOffSet() {
        return offSet;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPageSize() {
        return pageSize;
    }
    public Integer getRowNumFrom() {
        return offSet;
    }
    public Integer getRowNumTo() {
        return offSet+1+pageSize;
    }

}